package com.haizhi.workstation.mq;

import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQMapMessage;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;

@Component
public class MQSender {
    @Autowired
    private JmsTemplate jmsTemplate;

    /**
     * @param appOperationMessage
     */
    public void sendAppOperation(AppOperationMessage appOperationMessage){
        ObjectMessage objectMessage = new ActiveMQObjectMessage();
        try {
            objectMessage.setObject(appOperationMessage);
            jmsTemplate.convertAndSend(MQDestination.PUBLIC_OPERATION_DESTINATION,objectMessage);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    public void sendFileMessage(String  fileContent,String fileName){
        MapMessage mapMessage = new ActiveMQMapMessage();
        try {
            mapMessage.setObject("fileContent",fileContent);
            mapMessage.setObject("fileName",fileName);
            mapMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY,1*1000);
            jmsTemplate.convertAndSend(MQDestination.PUBLIC_FILE_CONTENT_DESTINATION,mapMessage);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}
